{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy.stats import gmean\n",
    "%matplotlib inline\n",
    "import seaborn as sns\n",
    "\n",
    "EXPERIMENT_DIR = 'PATH/TO/YOUR/EXPERIMENT'\n",
    "\n",
    "FILEPATH = os.path.join(EXPERIMENT_DIR,'solution_6_best_stacking/lightGBM_stacking_with_features_out_of_fold_test_predictions.csv')\n",
    "test_predictions = pd.read_csv(FILEPATH)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Arithmetic mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_prediction_ar_mean = test_predictions.groupby('SK_ID_CURR')['lightGBM_stacking_with_features_prediction'].apply(np.mean).reset_index()\n",
    "\n",
    "test_prediction_ar_mean.columns = ['SK_ID_CURR','TARGET']\n",
    "test_prediction_ar_mean[test_prediction_ar_mean['SK_ID_CURR']==100001]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Geometric mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_prediction_geom_mean = test_predictions.groupby('SK_ID_CURR')['lightGBM_stacking_with_features_prediction'].apply(gmean).reset_index()\n",
    "\n",
    "test_prediction_geom_mean.columns = ['SK_ID_CURR','TARGET']\n",
    "test_prediction_geom_mean[test_prediction_ar_mean['SK_ID_CURR']==100001]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Rank Mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_rank(predictions):\n",
    "    rank = (1 + predictions.rank().values) / (predictions.shape[0] + 1)\n",
    "    return rank\n",
    "\n",
    "test_predictions_with_ranks = []\n",
    "for fold_id, fold_df in test_predictions.groupby('fold_id'):\n",
    "    fold_df['lightGBM_rank'] = calculate_rank(fold_df['lightGBM_stacking_with_features_prediction'])\n",
    "    test_predictions_with_ranks.append(fold_df)\n",
    "test_predictions_with_ranks = pd.concat(test_predictions_with_ranks, axis=0)\n",
    "\n",
    "test_predictions_with_ranks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_prediction_rank_mean = test_predictions_with_ranks.groupby('SK_ID_CURR')['lightGBM_stacking_with_features_prediction'].apply(np.mean).reset_index()\n",
    "\n",
    "test_prediction_rank_mean.columns = ['SK_ID_CURR','TARGET']\n",
    "test_prediction_rank_mean[test_prediction_ar_mean['SK_ID_CURR']==100001]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
